package com.dwd.org.controller;

import com.dwd.org.pojo.Department;
import com.dwd.org.query.DepartmentQuery;
import com.dwd.org.service.IDepartmentService;
import com.dwd.basic.utils.AjaxResult;
import com.dwd.basic.utils.PageBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/5 11:38
 * @Version 1.0
 * @ApiOperation 方法注释
 */
@RestController
@RequestMapping(value = "/dept")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * @param department
     * @return
     * @RequestBody 接受json对象的数据  转换为java对象
     * 注意:如果不加@RequestBody对象不能接收数据
     */
    @PutMapping
    @ApiOperation("增加或者修改")
    public AjaxResult saveOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() != null) {
                departmentService.update(department);
            } else {
                departmentService.save(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation("根据id删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            System.out.println(id);
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询部门")
    public Department queryDepById(@PathVariable("id") Long id) {
        return departmentService.queryById(id);
    }


    @GetMapping
    @ApiOperation("查询全部部门")
    public List<Department> queryAllDeps() {
        return departmentService.queryAll();
    }

    @PostMapping
    @ApiOperation("分页查询")
    public PageBean<Department> queryPage(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryPage(departmentQuery);
    }

    @PostMapping("/batchRemove")
    @ApiOperation("批量删除")
    public AjaxResult batchRemove(@RequestBody List<Department> list) {
        try {
            departmentService.batchRemove(list);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    @PostMapping("/queryParent/{id}")
    @ApiOperation("修改上级部门")
    public List<Department> queryParent(@PathVariable("id") Long id) {
//        List<Department> departments = departmentService.queryparent(id);
        List<Department> departments = departmentService.queryparent4(id);
        return departments;
    }

    @PostMapping("/queryParent")
    @ApiOperation("增加上级部门")
    public List<Department> queryParent2() {
        List<Department> departments = departmentService.queryparent2();
        return departments;
    }

}
